<template>
  <div>
    <el-tree
      style="font-size: 14px"
      ref="leftTree"
      :data="deptList"
      node-key="id"
      :props="defaultProps"
      default-expand-all
      empty-text="暂无数据"
      :show-checkbox="false"
      :highlight-current="true"
      :expand-on-click-node="false"
      @node-click="handleNodeClick">
      <div class="custom-tree-node" slot-scope="{ node, data }">
        <div>
          <!-- 没有子元素时显示的图标 (即是最后一层) -->
          <span v-if="data.children.length == 0">
            <i
              class="el-icon-document"
              style="color: #8c8c8c; margin-right: 3px"></i>
          </span>
          <!-- 有子元素显示的图标 -->
          <span v-else @click.stop="openBtn(data)">
            <!-- 这里的展开的显示隐藏即是 [+] [-]-->
            <svg-icon
              v-if="data.open"
              icon-class="add-s"
              style="margin-right: 4px; font-size: 18px" />
            <svg-icon v-else icon-class="sub-s" style="margin-right: 4px" />
          </span>
          <!-- 名称 -->
          <span>{{ node.label }}</span>
        </div>
      </div>
    </el-tree>
  </div>
</template>

<script>
import { getDeptListApi } from '@/api/department'
export default {
  components: {},
  data() {
    return {
      //左侧部门树数据域
      deptList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getDeptList()
  },
  mounted() {},
  methods: {
    handleNodeClick(data) {
      // console.log(data)
      // this.parms.deptId = data.id
      // this.getUserList()
      this.$emit('treeClick', data)
    },
    //加减号图标点击事件
    openBtn(data) {
      console.log(data)
      data.open = !data.open
      this.$refs.leftTree.store.nodesMap[data.id].expanded = !data.open
    },
    // 获取左侧部门数据数
    async getDeptList() {
      let res = await getDeptListApi()
      // console.log(res)
      if (res && res.code === 200) {
        this.deptList = res.data
        // 树加载完成之后,默认点击第一个节点
        this.$nextTick(() => {
          const firstNode = document.querySelector('.el-tree-node')
          firstNode.click()
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-tree {
  // 将每一行的设置为相对定位 方便后面before after 使用绝对定位来固定位置
  .el-tree-node {
    position: relative;
    // padding-left: 10px;
  }
  // 子集像右偏移 给数线留出距离
  .el-tree-node__children {
    padding-left: 20px;
  }
  //这是竖线
  .el-tree-node :last-child:before {
    height: 40px;
  }
  .el-tree > .el-tree-node:before {
    border-left: none;
  }
  .el-tree > .el-tree-node:after {
    border-top: none;
  }
  //这自定义的线 的公共部分
  .el-tree-node:before,
  .el-tree-node:after {
    content: '';
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }
  .tree :first-child .el-tree-node:before {
    border-left: none;
  }
  // 竖线
  .el-tree-node:before {
    border-left: 1px solid #d9d9d9;
    bottom: 0px;
    height: 100%;
    top: -25px;
    width: 1px;
  }
  //横线
  .el-tree-node:after {
    border-top: 1px solid #d9d9d9;
    height: 20px;
    top: 14px;
    width: 12px;
  }
  .el-tree-node__expand-icon.is-leaf {
    width: 8px;
  }
  //去掉elementui自带的展开按钮  一个向下的按钮,打开时向右
  .el-tree-node__content > .el-tree-node__expand-icon {
    display: none;
  }
  //每一行的高度
  .el-tree-node__content {
    line-height: 30px;
    height: 30px;
    padding-left: 10px !important;
  }
}
//去掉最上级的before  after 即是去电最上层的连接线
::v-deep .el-tree > div {
  &::before {
    display: none;
  }
  &::after {
    display: none;
  }
}
</style>
